package com.example.grade;

public class Grade {
    private final Student student;
    private final Course course;
    private double score; // 0–100

    public Grade(Student student, Course course, double score) {
        if (student == null || course == null)
            throw new IllegalArgumentException("学生或课程不能为空");
        this.student = student;
        this.course = course;
        setScore(score); // 复用范围检查
    }

    public void setScore(double score) {
        if (score < 0 || score > 100)
            throw new ScoreRangeException(
                    String.format("分数 %.2f 超出范围[0,100]", score));
        this.score = score;
    }

    public Student getStudent() { return student; }
    public Course getCourse()  { return course; }
    public double getScore()   { return score; }

    /** 根据分数返回等级制成绩（4 分制） */
    public double toPoint() {
        if (score >= 90) return 4.0;
        if (score >= 85) return 3.7;
        if (score >= 82) return 3.3;
        if (score >= 78) return 3.0;
        if (score >= 75) return 2.7;
        if (score >= 72) return 2.3;
        if (score >= 68) return 2.0;
        if (score >= 66) return 1.7;
        if (score >= 64) return 1.3;
        if (score >= 60) return 1.0;
        return 0.0;
    }

    @Override
    public String toString() {
        return String.format("Grade{%s|%s|%.1f}", student.getName(), course.getName(), score);
    }
}